package com.climber.spb_rabbitmq.exchange.topic;

import com.climber.spb_rabbitmq.utils.RabbitMQUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.nio.charset.StandardCharsets;

public class TopicConsumer2 {
    private static String EXCHANGE_NAME="topic_logs";
    private static String QUEUE_NAME="Q2";
    public static void main(String[] args) {
        Channel channel = RabbitMQUtils.getChannel();
        try {
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"*.q2.#");
            System.out.println("waiting for message ......");
            DeliverCallback callback=(d,m)->{
                System.out.println(m.getEnvelope().getRoutingKey());
                System.out.println(new String(m.getBody(), StandardCharsets.UTF_8));
            };
            CancelCallback cancelCallback=(consumerTag)->{
                System.out.println(consumerTag);
                System.out.println("MConsumer1 receive interrupted");
            };
            channel.basicConsume(QUEUE_NAME,true,callback,cancelCallback);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
